package com.haier.cre.commonframe.utils;

import android.widget.Toast;

import com.haier.cre.commonframe.R;

import es.dmoral.toasty.Toasty;

/**
 * 项目名：CommonLibrary
 * 包 名：com.haier.cre.commonframe.utils
 * 创建人：18002063
 * 日 期：2018/10/16
 * 简 述：土司的工具类
 */
public class T {
    public static final char SUCCESS = 'A';
    public static final char ERROR = 'B';
    public static final char INFO = 'C';
    public static final char WARNING = 'D';
    public static final char NORMAL = 'E';

    private T() {
        /** cannot be instantiated**/
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 对toast的简易封装。线程安全，可以在非UI线程调用。
     */
    public static void showToast(final int resId, char type) {
        showToast(resId, Toast.LENGTH_SHORT, type);
    }

    /**
     * 对toast的简易封装。线程安全，可以在非UI线程调用。
     */
    public static void showToast(final int resId, int duration, char type) {
        showToast(CommonUtils.getText(resId), duration, type);
    }

    /**
     * 对toast的简易封装。线程安全，可以在非UI线程调用。
     */
    public static void showToast(final CharSequence msg, char type) {
        showToast(msg, Toast.LENGTH_SHORT, type);
    }

    /**
     * 对toast的简易封装。线程安全，可以在非UI线程调用。
     */
    public static void showToast(final CharSequence msg, final int duration, final char type) {
        if (CommonUtils.isRunInMainThread()) {
            show(msg, duration, type);
        } else {
            CommonUtils.post(new Runnable() {
                @Override
                public void run() {
                    show(msg, duration, type);
                }
            });
        }
    }


    /**
     * 需要判断是否是在前台的Activity ???
     */
    private static void show(CharSequence str, int duration, char type) {

        //        if (!isDurationTimeRight(duration)) {
        //            //对于输入错误的duration,一律为Toast.LENGTH_LONG
        //            duration = Toast.LENGTH_LONG;
        //        }

        switch (type) {
            case SUCCESS:
                Toasty.success(CommonUtils.getContext(), str, duration, true).show();
                break;
            case ERROR:
                Toasty.error(CommonUtils.getContext(), str, duration, true).show();
                break;
            case INFO:
                Toasty.info(CommonUtils.getContext(), str, duration, true).show();
                break;
            case WARNING:
                Toasty.warning(CommonUtils.getContext(), str, duration, true).show();
                break;
            case NORMAL:
                Toasty.normal(CommonUtils.getContext(), str, duration).show();
                break;
        }

        //        Toast.makeText(UIUtils.getContext(), str + "", duration).show();

    }

    private static boolean isDurationTimeRight(int duration) {
        return Toast.LENGTH_LONG == duration || Toast.LENGTH_SHORT == duration;
    }

    public static void reset() {
        Toasty.Config.reset();
    }

    public static void init() {
        Toasty.Config.getInstance()
                .setErrorColor(CommonUtils.getColor(R.color.toast_error)) // optional
                .setInfoColor(CommonUtils.getColor(R.color.toast_info)) // optional
                .setSuccessColor(CommonUtils.getColor(R.color.toast_success)) // optional
                .setWarningColor(CommonUtils.getColor(R.color.toast_warning)) // optional
                .setTextColor(CommonUtils.getColor(R.color.white)) // optional
                .apply(); // required
    }
}
